using System.Collections.Generic;
using CosyTrade.ClientForm.Financial;
using CosyTrade.ClientForm.Trade;
using CosyTrade.Core;
using CosyTrade.UI;
using System;
using CosyTrade.ClientForm.Distribution;
using CosyTrade.ClientForm.Store;
using CosyTrade.ClientForm.Organization;
using CosyTrade.ClientForm.Futues;

namespace CosyTrade.ClientForm
{
    [Addin("ModuleLoader")]
    public class ModuleLoader : AddinBase, IModuleLoader
    {
        private readonly Dictionary<string, FormBase> runningForm = new Dictionary<string, FormBase>();

        public FormBase LoadForm(string moduleKey)
        {
            return runningForm.ContainsKey(moduleKey) ? runningForm[moduleKey] : CreateForm(moduleKey);
        }

        private FormBase CreateForm(string moduleKey)
        {
            FormBase formBase = null;
            if (moduleKey.Equals("export_Draft", StringComparison.OrdinalIgnoreCase))
                formBase = new Export_DraftList();
            else if (moduleKey.Equals("crm_ClientCategory", StringComparison.OrdinalIgnoreCase))
                formBase = new FrmClientCategory();
            else if (moduleKey.Equals("crm_Client", StringComparison.OrdinalIgnoreCase))
                formBase = new FrmClientManager();
            else if (moduleKey.Equals("setting_Region", StringComparison.OrdinalIgnoreCase))
                formBase = new RegionManagerForm();
            else if (moduleKey.Equals("ProductCategory", StringComparison.OrdinalIgnoreCase))
                formBase = new ProductCategoryForm();
            else if (moduleKey.Equals("Product", StringComparison.OrdinalIgnoreCase))
                formBase = new ProductManagerForm();
            else if (moduleKey.Equals("setting_EmployeeManager", StringComparison.OrdinalIgnoreCase))
                formBase = new EmployeeManager();
            else if (moduleKey.Equals("setting_CompanyManager", StringComparison.OrdinalIgnoreCase))
                formBase = new CompanyManager();
            else if (moduleKey.Equals("ExchangeRate", StringComparison.OrdinalIgnoreCase))
                formBase = new ExchangeRateSettingForm();
            else if (moduleKey.Equals("Expenditure", StringComparison.OrdinalIgnoreCase))
                formBase = new ExpenditureForm();
            else if (moduleKey.Equals("BrowwerProduct", StringComparison.OrdinalIgnoreCase))
                formBase = new SellerLoanLogForm();
            else if (moduleKey.Equals("exportList_Unreceive", StringComparison.OrdinalIgnoreCase))
                formBase = new Export_Unreceive();
            else if (moduleKey.Equals("exportList_Finish", StringComparison.OrdinalIgnoreCase))
                formBase = new ExportList_Finish();
            else if (moduleKey.Equals("import_Shipping", StringComparison.OrdinalIgnoreCase))
                formBase = new Import_Shipping();
            else if (moduleKey.Equals("import_Countting", StringComparison.OrdinalIgnoreCase))
                formBase = new Import_Countting();
            else if (moduleKey.Equals("import_Finish", StringComparison.OrdinalIgnoreCase))
                formBase = new Import_Finish();
            else if (moduleKey.Equals("StoreCountting", StringComparison.OrdinalIgnoreCase))
                formBase = new CompanyStore();
            else if (moduleKey.Equals("PrintCategory", StringComparison.OrdinalIgnoreCase))
                formBase = new CategoryBookListForm();
            else if (moduleKey.Equals("SendMoney", StringComparison.OrdinalIgnoreCase))
                formBase = new RemitLog();
            else if (moduleKey.Equals("AccountBook", StringComparison.OrdinalIgnoreCase))
                formBase = new AccountBook();
            else if (moduleKey.Equals("Wage", StringComparison.OrdinalIgnoreCase))
                formBase = new LaborageCalculate();
            else if (moduleKey.Equals("new_futureorder", StringComparison.OrdinalIgnoreCase))
                formBase = new FuturesOrderList();
            else if (moduleKey.Equals("futures_orderlist", StringComparison.OrdinalIgnoreCase))
                formBase = new FormalFuturesOrder();
            else if (moduleKey.Equals("futures_finish", StringComparison.OrdinalIgnoreCase))
                formBase = new FinishFuturesOrder();
            #region Distrbution_Menu
            else if (moduleKey.Equals("Order_Draft", StringComparison.OrdinalIgnoreCase))
                formBase = new Order_DraftList();
            else if (moduleKey.Equals("Order_RealOrder", StringComparison.OrdinalIgnoreCase))
                formBase = new RealOrder_List();
            else if (moduleKey.Equals("Order_PackageFinish", StringComparison.OrdinalIgnoreCase))
                formBase = new PackageFinish_OrderList();
            else if (moduleKey.Equals("Order_Shipping", StringComparison.OrdinalIgnoreCase))
                formBase = new Shipping_OrderList();
            else if (moduleKey.Equals("OrderList_WattingPay", StringComparison.OrdinalIgnoreCase))
                formBase = new WaittingPay_OrderList();
            else if (moduleKey.Equals("OrderList_TimeoutPay", StringComparison.OrdinalIgnoreCase))
                formBase = new TimeoutPay_OrderList();
            else if (moduleKey.Equals("OrderList_Finish", StringComparison.OrdinalIgnoreCase))
                formBase = new Finish_OrderList();
            else if (moduleKey.Equals("Order_Search", StringComparison.OrdinalIgnoreCase))
                formBase = new SearchOrderForm();
            else if (moduleKey.Equals("Order_Reclaim", StringComparison.OrdinalIgnoreCase))
                formBase = new OrderReclaimForm();
            #endregion
            else if (moduleKey.Equals("ExpenditureType", StringComparison.OrdinalIgnoreCase))
                formBase = new FrmExpendType();

            if (formBase != null)
            {
                formBase.ModuleKey = moduleKey;
                formBase.FormClosed +=formBase_FormClosed;
                runningForm.Add(moduleKey, formBase);
            }
            return formBase;
        }

        private void formBase_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
        {
            lock (this)
                if (runningForm.ContainsKey(((FormBase) sender).ModuleKey))
                    runningForm.Remove(((FormBase) sender).ModuleKey);
        }
    }
}